package com.nstar.helloworld.commands
{
	import mx.collections.ArrayCollection;
	import org.osflash.arp.AMF0PendingCall;
	import org.osflash.arp.AMF0Service;
	import org.osflash.arp.AMF0RelayResponder;
	import mx.managers.CursorManager;
	import mx.utils.ObjectUtil;
	import org.osflash.arp.ModelLocator;
	import flash.events.MouseEvent;
	import mx.controls.Alert;
	import com.nstar.base.util.Logger;
	import com.nstar.helloworld.model.Model;
	import com.nstar.helloworld.events.QueryByIdEvent;
	import com.nstar.business.ServiceLocator;
	
	public class QueryByIdCommand
	{
		private var viewRef:*;
		public var log:Logger = Logger.getInstance();	
		
		var model:Model = Model.getInstance();	
		
		
		////////////////////////////////////////////////////////////////////////////
		//
		// Public Methods
		//
		////////////////////////////////////////////////////////////////////////////
		public function execute( viewRef:*, event:QueryByIdEvent ):void
		{
			log.debug("QueryByIdCommand.execute()..");
			this.viewRef = viewRef;
			
			if (model.isDebug){
				log.debug("调试状态：直接调用 QueryByIdCommand.onResult()方法...");
				var ary:ArrayCollection = new ArrayCollection();
				for(var i:int = 0;i<15;i++){
					var obj:Object = new Object();
					obj.userName = "name"+i;
					obj.departName = "departName"+i;
					obj.addr = "addr"+i;
					obj.telephone = "telephone"+i;
					obj.mobile = "mobile"+i;
					obj.email = "email"+i;
					obj.qq = "qq"+i;
					
					ary.addItem(obj);
					
				}
				onResult(ary);
				return;
			}	
			var service:AMF0Service = ServiceLocator.getInstance().getService("userServices");
			var pendingCall:AMF0PendingCall = service.findUserInfoByUserId(event.request);
			pendingCall.responder = new AMF0RelayResponder(this, "onResult", "onStatus");
			CursorManager.setBusyCursor();	
		}
		
		public function onResult (result:*):void 
		{
			CursorManager.removeBusyCursor();
			log.debug("QueryByIdCommand.onResult() 执行完成.");
			
			var ary:ArrayCollection = new ArrayCollection();
			if (result != null){
				var obj:Object = translate(result);
				ary.addItem(obj);
			}		
			//刷新数据
			viewRef.refresh(ary);
		}
		
		public function onStatus (faultObj:Object):void 
		{
			CursorManager.removeBusyCursor();
			throw new Error ("QueryByIdCommand 执行失败 , details="+faultObj.details);			
		}
	
	
		
	
		////////////////////////////////////////////////////////////////////////////
		// Event Handlers
		////////////////////////////////////////////////////////////////////////////
	
		
	
		////////////////////////////////////////////////////////////////////////////
		// Getter/Setters
		////////////////////////////////////////////////////////////////////////////
	
		
			
		////////////////////////////////////////////////////////////////////////////
		//
		// Private Methods
		//
		////////////////////////////////////////////////////////////////////////////			
		private function translate(item:Object):Object{
			switch (item.department.departId){
				case "A":
					item.departName = "市场部";
					break;
				case "B":
					item.departName = "软件开发部";
					break;
				case "C":
					item.departName = "软件测试部";
					break;
				case "D":
					item.departName = "无线事业部";
					break;
			}
			return item;
		}		

	}
}